package com.zhang.es;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @program: elasitcsearch
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-09
 **/
public class EsSearch {
    private TransportClient client;

    @Before
    public void init() throws Exception {
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
        client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
    }

    @Test
    public void getById() {
        //1:创建一个client对象--查询方式
        QueryBuilder QueryBuilder = QueryBuilders.idsQuery().addIds("9", "10");
        //2:创建一个查询对象
        SearchResponse searchResponse = client.prepareSearch("java1")
            .setTypes("user")
            .setQuery(QueryBuilder)
            //3:执行查询
            .get();

        //4:取出查询结果
        SearchHits hits = searchResponse.getHits();

        //5:取出查询结果的总记录数字
        System.out.println("共查询记录" + hits.getTotalHits() + "条");
        //6:查询结果列表
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            SearchHit next = iterator.next();
            //打印文档对象
            System.out.println(next.getSourceAsString());
            //取出文档的属性
            System.out.println("------取出文档的属性st------");
            Map<String, Object> source = next.getSource();
            System.out.println(source.get("id") + "id编号");
            System.out.println(source.get("userName") + "userName");
            System.out.println("------取出文档的属性ed------");
        }

        //7:关闭client
        client.close();
    }
    @Test
    public void getByTerm() throws Exception{
        QueryBuilder builder = QueryBuilders.termQuery("userName","领主");
        SearchResponse java1 = client.prepareSearch("java1").setTypes("user").setQuery(builder).get();
        SearchHits hits = java1.getHits();
        System.out.println("共查询记录" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()){
            SearchHit next = iterator.next();
            System.out.println(next.getSourceAsString());
            Map<String, Object> source = next.getSource();
            System.out.println(source.get("id")+ "id编号");
            System.out.println(source.get("userName") + "userName");
        }
        client.close();
    }
    @Test
    public void getByQuertString(){
        QueryBuilder builder = QueryBuilders.queryStringQuery("领主的意志").defaultField("userName");

        SearchResponse searchResponse = client.prepareSearch("java1")
            .setTypes("user")
            .setQuery(builder)
            .get();
        SearchHits hits = searchResponse.getHits();
        System.out.println("共查询记录" + hits.getTotalHits() + "条");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
            Map<String, Object> source = hit.getSource();
            System.out.println(source.get("id"));
            System.out.println(source.get("userName"));
        }
        client.close();
    }
    @Test
    public void searchByPage(){
        QueryBuilder builder = QueryBuilders.queryStringQuery("领主的意志").defaultField("userName");
        SearchResponse searchResponse = client.prepareSearch("java1")
            .setTypes("user")
            .setQuery(builder)
            .setFrom(0)
            .setSize(5)
            .get();
        SearchHits hits = searchResponse.getHits();
        System.out.println("共查询记录" + hits.getTotalHits() + "条");
        System.out.println("共查询记录" + hits.getTotalHits()/5 + "页");
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
            Map<String, Object> source = hit.getSource();
            System.out.println(source.get("id"));
            System.out.println(source.get("userName"));
        }
        client.close();
    }
    @Test
    public void seachHigh(){
        HighlightBuilder highlight = new HighlightBuilder()
                                            .field("userName")
                                            .preTags("<span>")
                                            .postTags("<span>");


        QueryBuilder builder = QueryBuilders.queryStringQuery("领主的意志").defaultField("userName");
        SearchResponse searchResponse = client.prepareSearch("java1")
            .setTypes("user")
            .setFrom(0)
            .setSize(5)
            .setQuery(builder)
            .highlighter(highlight)
            .get();
        SearchHits hits = searchResponse.getHits();
        hits.forEach((s)->{
            //System.out.println( s.getSourceAsString() ); 原始数据

            Map<String, HighlightField> highlightFields = s.getHighlightFields();
            System.out.println(highlightFields);
            Text[] userNames = highlightFields.get("userName").fragments();
            for (Text userName : userNames) {
                System.out.println(userName);
            }
        });
    }
}
